import { objDeepCopy,getDateString } from "@/assets/js/common.js";
import { parse } from "path";
let Controller = {
	created() {
		this.getSplitTypeList()
	},
	methods: {
		init() {

		},
		checkSku(){
			let model = this.Model
			model.importType = 1
			
			this.$ajax({
				method: 'get',
				url:`api/materialDismantling/goodsInfo/get/${model.inputBarcode}`,
			}).then(res => {
				let data = res.data
				if(data.status == 200){
					this.disposeResultData(data.result)
				}
			})
		},
		//提交校验
		submitVerify(){
			let model = this.Model
			if(!model.splitType){
				this.$message.error('请选择拆解单类型')
				return
			}
			if(!model.tableDataOut.length){
				this.$message.error('请导入商品')
				return
			}
			if(!model.tableDataOut_two.length){
				this.$message.error('请导入拆解物料')
				return
			}
			if(model.tableDataOut_two.find(item=>!item.barCode)){
				this.$message.error('请先获取条码')
				return
			}
			return true
		},
		//提交审批
		submit(){
			let model = this.Model
			let params = {
				operatorType: 2,//1:保存，2：审核，目前只做审核,都发送2
				dismantlingOrderType:model.splitType,
				dismantlingReasons:model.dismantlingReasons,
				remark:model.remark,
				outgoingWarehouse:model.outgoingWarehouse,
				warehouse:model.warehouse
			}
			//校验
			if(!this.submitVerify()){
				return
			}
			let goodsBarCodes = []
			for(let item of model.tableDataOut){
				goodsBarCodes.push(item.batchNo+'')
			}
			let materialBarCodes = []
			for(let item of model.tableDataOut_two){
				materialBarCodes.push(item.barCode+'')
			}
			params.goodsBarCodes = goodsBarCodes
			params.materialBarCodes = materialBarCodes
			this.$ajax({
				method: 'post',
				url:`api/materialDismantling/submit`,
				data:params
			}).then((res)=>{
				let data = res.data
				if(data.status == 200){
					
				}else{
					this.$message.error(data.msg||'请求失败')
				}
			});
		},
		/**
		 * 打开导入弹窗
		 * @param {Number} type 导入类型 1导入商品 2导入拆解物料
		 */
		openImportPop(type){
			let model = this.Model
			//校验
			switch(parseInt(type)){
				case 1:
				break;
				case 2:
				if(!model.splitType){
					this.$message.error('请选择拆解单类型')
					return
				}
				if(!model.tableDataOut.length){
					this.$message.error('请先导入商品')
					return
				}
				break;
			}
			model.importFileDialog = true
			model.importType = type
		},
		//获取条码
		getBarcode(){
			let tableList = this.Model.tableDataOut_two
			this.$ajax({
				method: 'post',
				url:`api/materialDismantling/barcodeGeneration`,
				data:tableList
			}).then((res)=>{
				let data = res.data
				if(data.status == 200){
					// this.Model.tableDataOut_two = data.result
					this.$set(this.Model,tableDataOut_two,data.result)
				}else{
					this.$message.error(data.msg||'请求失败')
				}
			});
		},
		/**
		 * 处理返回的数据
		 * @param {*} result 返回的结果
		 */
		disposeResultData(result){
			let model = this.Model
			switch(model.importType){
				case 1:
				this.$set(model,'tableDataOut',model.tableDataOut.concat(result))
				break;
				case 2:
				this.$set(model,'tableDataOut_two',model.tableDataOut_two.concat(result))
				break;
			}
		},
		//点击上传
		uploadExcel(){
			var apiTest = this.apiTest;
			let model = this.Model
			//区分导入商品或导入物料
			let requestUrl = `api/materialDismantling/import`
			if(model.importType==1){
				requestUrl = `api/materialDismantling/importGoods`
			}
    		this.$ajax({
				method: 'post',
				url:requestUrl,
				data:model.upFileForm
			}).then((res)=>{
				let data = res.data
				if(data.status == 200){
					this.disposeResultData(data.result)
					model.importFileDialog=false
				}else{
					this.tips(this,'warning','导入失败！')
				}
			});
			document.getElementById("fileup").value=''
			model.upFileForm = ''
			model.fileupName = ''
		},
		//选择文件
		fileChange(){
			var fileObj = document.getElementById("fileup").files[0];
			var form = new FormData();
			let model = this.Model
			form.append("file", fileObj);
			switch(model.importType){
				case 1:
				break;
				case 2:
				form.append("dismantlingOrderType", model.splitType);
				form.append("barCodes", this.getItemBarcode());
				break;
			}
			model.upFileForm = form;
			model.fileupName = fileObj.name
		},
		//获取商品barcode
		getItemBarcode(){
			let tableList = this.Model.tableDataOut
			let barcodeList = []
			for(let item of tableList){
				barcodeList.push(item.batchNo)
			}
			return barcodeList.join(',')
		},
		//获取拆解单类型
		getSplitTypeList(){
			this.$ajax({
				method:'get',
				url:`api/materialDismantling/dismantlingOrderType/list`
			}).then(res=>{
				let data = res.data
				if(data.status == 200 && data.result){
					this.Model.splitTypeOption = data.result
				}else{
					this.$message.error(data.mes)
				}
			})
		},
		//取消
		goBack() {
			this.$router.back(-1);
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		handleCurrentChange(val) {
			this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
			this.Model.pager.params.pageSize = val;
		},
		tips(that,type,message) {
			that.$message({
				message: message,
				type: type
			});
		},
	},
}
export default Controller;
